package a66weding.com.jiehuntong.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.List;

import a66weding.com.jiehuntong.R;
import a66weding.com.jiehuntong.beans.GuestBean;
import a66weding.com.jiehuntong.common.utils.Tools;

/**
 * Created by Administrator on 2016/12/2.
 */
public class GuestAdaputer extends BaseAdapter {
    Context context;
    List<GuestBean.DataBean> data;
    RemoveCallBack rcb;

    public interface RemoveCallBack{
        public void removeGuest(GuestBean.DataBean bean,int  position);
    }
    public GuestAdaputer(Context context, List<GuestBean.DataBean> mGuestDateBean,RemoveCallBack rcb) {
        this.context = context;
        this.rcb =rcb;
        if (mGuestDateBean!=null){
            this.data = mGuestDateBean;
        }
    }
    public void clear() {
        if (data != null) {
            this.data.clear();
        }
    }
    public void remove(int position){
        if (data != null&&position<data.size()) {
           data.remove(position);
//           rcb.removeGuest(data.get(position),position);
           notifyDataSetChanged();
        }
    }
    public void append(List<GuestBean.DataBean> data) {
        if (this.data != null) {
            this.data.addAll(data);
        } else {
            this.data = data;
        }
        notifyDataSetChanged();
    }
    @Override
    public int getCount() {
        return data.size();
    }
    @Override
    public Object getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView( int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView==null){
            convertView = View.inflate(context, R.layout.item_guests, null);
            viewHolder=new ViewHolder();
            viewHolder.tv_guest_name = (TextView) convertView.findViewById(R.id.tv_guest_name);
             viewHolder.tv_attendance = (TextView) convertView.findViewById(R.id.tv_attendance);
             viewHolder.tv_guest_blessing = (TextView) convertView.findViewById(R.id.tv_guest_blessing);
            viewHolder. guest_head = (ImageView) convertView.findViewById(R.id.guest_head);
            viewHolder. img_remove_guests = (ImageView) convertView.findViewById(R.id.img_remove_guests);
            convertView.setTag(viewHolder);
        }else {
            viewHolder= (ViewHolder) convertView.getTag();
        }
        GuestBean.DataBean dataBean = data.get(position);
        if(!TextUtils.isEmpty(dataBean.getNvc_Photo())){
            Glide.with(context).load(dataBean.getNvc_Photo()).into(viewHolder.guest_head);
        }
        TextView guestName = viewHolder.tv_guest_name;
        guestName.setText(dataBean.getNvc_Name());//宾客姓名
        TextView guestBlessing = viewHolder.tv_guest_blessing;//宾客祝福
        guestBlessing.setText(dataBean.getNvc_Content());
        TextView mAttendance = viewHolder.tv_attendance;//人数
        mAttendance.setText(dataBean.getI_Attendance() + "人参加");
        ImageView  img_remove_guests= viewHolder.img_remove_guests;
        img_remove_guests.setTag(position);
        img_remove_guests.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                Tools.showSimpleDialog(context, "确认删除此宾客", "取消", "删除", new Tools.ClickListenerInterface(){
                    @Override
                    public void doConfirm() {
                        rcb.removeGuest(data.get((int)v.getTag()),(int)v.getTag());
                    }
                    @Override
                    public void doCancel() {

                    }
                });
            }
        });
        return convertView;
    }

    class ViewHolder{
        TextView tv_guest_name;
        TextView tv_attendance;
        TextView tv_guest_blessing ;
        ImageView guest_head;
        ImageView img_remove_guests;
    }
}
